import {
  SAVE_TOKEN,
  FETCH_USER_SUCCESS,
  FETCH_USER_FAILURE,
  LOGOUT
} from './mutation-types.js';

import {
  apiUser, logoutPost
} from '../services/index';

export default {
  saveToken ({commit}, payload) {
    commit(SAVE_TOKEN, payload);
  },
  async fetchUser ({
    commit
  }, token) {
    try {
      let userInfo = await apiUser();
      commit(FETCH_USER_SUCCESS, userInfo);
    } catch (e) {
      commit(FETCH_USER_FAILURE);
    }
  },
  async logout ({commit}) {
    await logoutPost();
    commit(LOGOUT);
  },
  ToggleSideBar: ({ commit }) => {
    commit('TOGGLE_SIDEBAR');
  }
};
